package com.likai.authority.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.likai.authority.dao.AuthorityMapper;
import com.likai.authority.dao.RoleMapper;
import com.likai.authority.pojo.Authority;
import com.likai.authority.pojo.TreeNode;
import com.likai.authority.service.IAuthorityService;
import com.likai.authority.util.AnswerInfo;
import com.likai.authority.util.IdUtil;

/**
 * AuthorityService实现类
 * @author likai
 * @since 2018-04-02
 */
@Service("authorityService")
public class AuthorityServiceImpl implements IAuthorityService {

	@Autowired
	private AuthorityMapper authorityMapper ;
	
	@Autowired
	private RoleMapper roleMapper ;
	
	/**
	 * 根据parentId查询列表
	 * @param parentId
	 * @return
	 */
	@Transactional(readOnly=true)
	public List<TreeNode> findByParentId(String parentId,String [] authIds) {
		List<TreeNode> treeList = new ArrayList<TreeNode>() ;
		//根据parentId查询列表信息
		List<Authority> authList = authorityMapper.selectByParentId(parentId,authIds) ;
		if(authList != null && authList.size() != 0) {
			for (Authority authority : authList) {
				TreeNode node = new TreeNode() ;
				node.setId(authority.getAuthid());
				node.setText(authority.getAuthname());
				node.setIconCls(authority.getIconcls());
				if(!hasChilden(authority.getAuthid(), authIds)) {
					node.setState("open");
				} else {
					node.setState(authority.getState());					
				}
				if(authority.getAuthpath() != null && !"".equals(authority.getAuthpath())) {
					node.setAttributes(authority.getAuthpath());					
				}
				treeList.add(node) ;
			}
		}
		return treeList;
	}
	
	@Transactional(readOnly=true)
	public List<TreeNode> findBySqParentId(String parentId,String [] authIds) {
		List<TreeNode> treeList = new ArrayList<TreeNode>() ;
		//根据parentId查询列表信息
		List<Authority> authList = authorityMapper.selectBySqParentId(parentId) ;
		if(authList != null && authList.size() != 0) {
			for (Authority authority : authList) {
				TreeNode node = new TreeNode() ;
				node.setId(authority.getAuthid());
				node.setText(authority.getAuthname());
				node.setIconCls(authority.getIconcls());
				node.setState(authority.getState());
				if(authIds != null && authIds.length != 0) {
					for(int i = 0 ; i < authIds.length ; i ++) {
						if(authIds[i].equals(authority.getAuthid())) {
							node.setChecked(true);
							break ;
						}
					}
				}
				if(authority.getAuthpath() != null && !"".equals(authority.getAuthpath())) {
					node.setAttributes(authority.getAuthpath());					
				}
				treeList.add(node) ;
			}
		}
		return treeList;
	}
	
	
	@Transactional(readOnly=true)
	public List<TreeNode> findByauthTreeParentId(String parentId) {
		List<TreeNode> treeList = new ArrayList<TreeNode>() ;
		//根据parentId查询列表信息
		List<Authority> authList = authorityMapper.selectBySqParentId(parentId) ;
		if(authList != null && authList.size() != 0) {
			for (Authority authority : authList) {
				TreeNode node = new TreeNode() ;
				node.setId(authority.getAuthid());
				node.setText(authority.getAuthname());
				node.setIconCls(authority.getIconcls());
				
				node.setState(authority.getState());
				if(authority.getAuthpath() != null && !"".equals(authority.getAuthpath())) {
					node.setAttributes(authority.getAuthpath());					
				}
				node.setAuthdescription(authority.getAuthdescription()) ;
				treeList.add(node) ;
			}
		}
		return treeList;
	}
	
	/**
	 * 查询导航树菜单
	 */
	@Transactional(readOnly=true)
	public List<TreeNode> findMenu(String parentId,String [] authIds) {
		//根据parentId查询树对象列表信息(List<TreeNode>)
		List<TreeNode> nodeList = this.findByParentId(parentId,authIds) ;
		List<TreeNode> treeList = new ArrayList<TreeNode>() ;
		//遍历
		for(int i = 0,l = nodeList.size(); i < l; i ++) {
			TreeNode treeNode = nodeList.get(i) ;
			treeList.add(treeNode) ;
			//如果state为open 代表为根节点
			if("open".equals(treeNode.getState())) {
				continue ;
			} else {
				//如果state为close 代表为父节点，递归重复调用
				treeNode.setChildren(findMenu(treeNode.getId(),authIds)) ;
			}
		}
		
		return treeList ;
	}

	public List<TreeNode> findSqMenu(String parentId, String[] authIds) {
		//根据parentId查询树对象列表信息(List<TreeNode>)
		List<TreeNode> nodeList = this.findBySqParentId(parentId,authIds) ;
		List<TreeNode> treeList = new ArrayList<TreeNode>() ;
		//遍历
		for(int i = 0,l = nodeList.size(); i < l; i ++) {
			TreeNode treeNode = nodeList.get(i) ;
			treeList.add(treeNode) ;
			//如果state为open 代表为根节点
			if("open".equals(treeNode.getState())) {
				continue ;
			} else {
				//如果state为close 代表为父节点，递归重复调用
				treeNode.setChildren(findSqMenu(treeNode.getId(),authIds)) ;
			}
		}
		
		return treeList ;
	}
	
	
	public List<TreeNode> findTreeGrid(String parentId) {
		//根据parentId查询树对象列表信息(List<TreeNode>)
		List<TreeNode> nodeList = this.findByauthTreeParentId(parentId) ;
		List<TreeNode> treeList = new ArrayList<TreeNode>() ;
		//遍历
		for(int i = 0,l = nodeList.size(); i < l; i ++) {
			TreeNode treeNode = nodeList.get(i) ;
			treeList.add(treeNode) ;
			//如果state为open 代表为根节点
			if("open".equals(treeNode.getState())) {
				continue ;
			} else {
				//如果state为close 代表为父节点，递归重复调用
				treeNode.setChildren(findTreeGrid(treeNode.getId())) ;
			}
		}
		
		return treeList ;
	}

	public int updateAuth(Authority po) {
		boolean flag = false ;
		String authid = po.getAuthid() ;
		if(authid == null || "".equals(authid)) {
			po.setAuthid(IdUtil.uuid());
			po.setState("open");
			//查询父节点是否是叶子节点
			if(isLeaf(po.getParentid())) {
				//是 需要更新父节点的state 为closed
				Authority parentPo = new Authority() ;
				parentPo.setAuthid(po.getParentid());
				parentPo.setState("closed");
				if(authorityMapper.updateByPrimaryKeySelective(parentPo) == 1) {
					if(authorityMapper.insertSelective(po) == 1) {
						flag = true ;
					}
				}
			} else {
				//不是
				if(authorityMapper.insertSelective(po) == 1) {
					flag = true ;
				}
			}
			
		} else {
			po.setParentid(null);
			if(authorityMapper.updateByPrimaryKeySelective(po) == 1) {
				flag = true ;
			}
		}
		if(flag) {
			return 1 ; 
		} else {
			return 0 ;
		}
	}
	
	public boolean isLeaf(String authid) {
		List<Authority> list = authorityMapper.selectBySqParentId(authid) ;
		if(list == null || list.size() == 0) {
			return true ;
		}else {
			return false ;
		}
	}
	
	/**
	 * 菜单认证
	 */
	public boolean hasChilden(String parentId,String [] authIds) {
		List<Authority> authList = authorityMapper.selectByParentId(parentId,authIds) ;
		if(authList == null || authList.size() == 0) {
			return false ;
		} else {
			return true ;
		}
	}

	public AnswerInfo removeAuth(String authId, String parentId) {
		AnswerInfo json = new AnswerInfo() ;
		int sonNum = 0 ;
		//要删除的节点是否存在子节点
		if(isLeaf(authId)) {
			//是子节点
			sonNum = Integer.parseInt(authorityMapper.selectCountParentId(parentId));
			if(sonNum == 1) {
				//修改父节点状态
				Authority parentPo = new Authority() ;
				parentPo.setState("open");
				parentPo.setAuthid(parentId);
				if(authorityMapper.updateByPrimaryKeySelective(parentPo) == 1) {
					//删除子节点
					if(authorityMapper.deleteByPrimaryKey(authId) == 1) {
						json.setData("删除成功");
					} else {
						json.setData("删除失败");
					}
				}
			} else {
				//删除子节点
				if(authorityMapper.deleteByPrimaryKey(authId) == 1) {
					json.setData("删除成功");
				} else {
					json.setData("删除失败");
				}
			}
		} else {
			//不是子节点
			json.setData("此节点存在子节点，不能删除");
		}
		return json;
	}
}
